package com.foo.AllRealtimeDataProvider;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.foo.DAO.AllRealTimeDAO;
import com.foo.DAO.RealtimeDAO;
import com.foo.DataProvider.ProviderInterface;
import com.foo.DataSender.DataSender;

/**
 * Session Bean implementation class AllRealTimeData
 */
@Stateless
public class AllRealTimeData implements ProviderInterface
{
	int total =0 ;
	//Query getanalyzeddata = null;
	String realtimemessage="";
	DataSender datasender = new DataSender();
	 
    /**
     * Default constructor. 
     */
    public AllRealTimeData() 
    {
        System.out.println("AllRealTimeData Constructor.");
    }

	@Override
	public void getData(String exampleName, String value, Timestamp timestamp, Date fromdate, Date todate) 
	{			
		
		/*if(exampleName.equals("AllRealTimeData"))
		{
			SessionFactory SF = new Configuration().configure().buildSessionFactory();
			Session session = SF.openSession();
			session.beginTransaction();
			
			System.out.println("In top volume condition.");
			analyzedmessage="";
			analyzeddatalist = (List<RealTimeData>)((session.createSQLQuery("select distinct * from realtimestockdata Group by ticker_symbol order by ticker_symbol")
					.addEntity(RealTimeData.class)).list());
				
			session.getTransaction().commit();
			session.close();
			
			System.out.println("here!");
			
			int listsize = analyzeddatalist.size();
			System.out.println("list size "+listsize);
			for(int k=0;k<listsize;k++)
			{
				analyzedmessage += analyzeddatalist.get(k).getTicker_symbol() + ":" +analyzeddatalist.get(k).getOpen_value()+" , ";
			}	
		}
		else
		{
			analyzedmessage="";
		}
		datasender.sendMessage("Analyzed",analyzedmessage);
		System.out.println("The message " +analyzedmessage);*/
		
		System.out.println("Server side: if condition :");
		// Example for using named query

		ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");
		AllRealTimeDAO forumDAO = (AllRealTimeDAO) context.getBean("allrealtimeDAO");   		
		realtimemessage=forumDAO.selectForum();		

		System.out.println("All Realtime The message " + realtimemessage);
		datasender.sendMessage("Analyzed",realtimemessage);
	}
}
